home *** CD-ROM | disk | FTP | other *** search
- class SideScroller.BasicCamera
- {
- var nStageWidth;
- var nStageHeight;
- var oParent;
- var nMode;
- var nFollowTargetPositionX;
- var nFollowTargetPositionY;
- var nFollowAddonX;
- var nFollowAddonY;
- var nMaxSpeed;
- var nAcceleration;
- var nCurrentX;
- var nCurrentY;
- var nSpeedX;
- var nSpeedY;
- var bFirstStartUp;
- var bPaused;
- var oLockedObject;
- var nTargetX;
- var nTargetY;
- static var MODE_LOCK = 1;
- static var MODE_FREE = 2;
- static var BUFFER_SCREEN_LEFT = 50;
- static var BUFFER_SCREEN_TOP = 50;
- static var BUFFER_SCREEN_RIGHT = 50;
- static var BUFFER_SCREEN_BOTTOM = 50;
- static var BUFFER_FOLLOW = 4;
- static var DEFAULT_FOLLOW_CENTER_X = 150;
- static var DEFAULT_FOLLOW_CENTER_Y = 150;
- static var SPEED_MAX = 25;
- static var ACCELERATION = 30;
- function BasicCamera(__nStageWidth, __nStageHeight, __oParent)
- {
- this.nStageWidth = __nStageWidth;
- this.nStageHeight = __nStageHeight;
- this.oParent = __oParent;
- this.nMode = SideScroller.BasicCamera.MODE_FREE;
- this.nFollowTargetPositionX = SideScroller.BasicCamera.DEFAULT_FOLLOW_CENTER_X;
- this.nFollowTargetPositionY = SideScroller.BasicCamera.DEFAULT_FOLLOW_CENTER_Y;
- this.nFollowAddonX = 0;
- this.nFollowAddonY = 0;
- this.nMaxSpeed = SideScroller.BasicCamera.SPEED_MAX;
- this.nAcceleration = SideScroller.BasicCamera.ACCELERATION;
- this.nCurrentX = 0;
- this.nCurrentY = 0;
- this.nSpeedX = 0;
- this.nSpeedY = 0;
- this.bFirstStartUp = true;
- this.bPaused = false;
- this.oParent.doAddListener(this);
- }
- function doPause()
- {
- this.bPaused = true;
- }
- function doResume()
- {
- this.bPaused = false;
- }
- function setFreeMode()
- {
- this.nMode = SideScroller.BasicCamera.MODE_FREE;
- delete this.oLockedObject;
- }
- function doLockOn(__oElement)
- {
- this.nMode = SideScroller.BasicCamera.MODE_LOCK;
- this.oLockedObject = __oElement;
- if(this.bFirstStartUp)
- {
- this.bFirstStartUp = false;
- this.doMoveTo(this.oLockedObject.Ref._x,this.oLockedObject.Ref._y);
- }
- }
- function doEnterFrame()
- {
- if(!this.bPaused)
- {
- if(this.nMode == SideScroller.BasicCamera.MODE_LOCK)
- {
- this.nTargetX = this.oLockedObject.Ref._x - SideScroller.BasicCamera.BUFFER_SCREEN_LEFT - this.nFollowTargetPositionX - this.nFollowAddonX;
- this.nTargetY = this.oLockedObject.Ref._y - SideScroller.BasicCamera.BUFFER_SCREEN_TOP - this.nFollowTargetPositionY - this.nFollowAddonY;
- }
- this.nSpeedX = (this.nTargetX - this.nCurrentX) / (100 / this.nAcceleration);
- this.nSpeedY = (this.nTargetY - this.nCurrentY) / (100 / this.nAcceleration);
- if(Math.abs(this.nSpeedX) > this.nMaxSpeed)
- {
- this.nSpeedX = this.nMaxSpeed * Library.Utils.MoreMath.getPolarity(this.nSpeedX);
- }
- if(Math.abs(this.nSpeedY) > this.nMaxSpeed)
- {
- this.nSpeedY = this.nMaxSpeed * Library.Utils.MoreMath.getPolarity(this.nSpeedY);
- }
- if(Math.abs(this.nSpeedX) < 0.2)
- {
- this.nSpeedX = 0;
- this.nCurrentX = this.nTargetX;
- }
- if(Math.abs(this.nSpeedY) < 0.2)
- {
- this.nSpeedY = 0;
- this.nCurrentY = this.nTargetY;
- }
- this.doMoveTo(this.nCurrentX + this.nSpeedX,this.nCurrentY + this.nSpeedY);
- }
- }
- function doTravelTo(__nX, __nY)
- {
- this.setFreeMode();
- this.nTargetX = __nX;
- this.nTargetY = __nY;
- }
- function doMoveTo(__nX, __nY)
- {
- if(__nX < this.oParent.Limits.left)
- {
- __nX = this.oParent.Limits.left;
- }
- if(__nX > this.oParent.Limits.right - this.nStageWidth)
- {
- __nX = this.oParent.Limits.right - this.nStageWidth;
- }
- if(__nY < this.oParent.Limits.top)
- {
- __nY = this.oParent.Limits.top;
- }
- if(__nY > this.oParent.Limits.bottom - this.nStageHeight)
- {
- __nY = this.oParent.Limits.bottom - this.nStageHeight;
- }
- this.nCurrentX = __nX;
- this.nCurrentY = __nY;
- }
- function doDestroy()
- {
- this.oParent.doRemoveListener(this);
- delete this.oParent;
- delete this.oLockedObject;
- }
- function get PosX()
- {
- return - this.nCurrentX;
- }
- function get PosY()
- {
- return - this.nCurrentY;
- }
- function set MaxSpeed(__nSpeed)
- {
- this.nMaxSpeed = __nSpeed;
- }
- function set Acceleration(__nAcceleration)
- {
- this.nAcceleration = __nAcceleration;
- }
- function set FollowAddonX(__nFollowAddonX)
- {
- this.nFollowAddonX = __nFollowAddonX;
- }
- function get FollowAddonX()
- {
- return this.nFollowAddonX;
- }
- function set FollowAddonY(__nFollowAddonY)
- {
- this.nFollowAddonY = __nFollowAddonY;
- }
- function get FollowAddonY()
- {
- return this.nFollowAddonY;
- }
- }
-